
#include "TitleLayer.h"
#include "GameScene.h"

USING_NS_CC;

bool TitleLayer::init() {


	CCLayer::init();

	this->setTouchEnabled(true);

	CCSize size = CCDirector::sharedDirector()->getVisibleSize();

	CCSprite* pBg = CCSprite::create("opening_bg@2x.jpg");
	pBg->setAnchorPoint(ccp(0, 0));
	pBg->setPosition(CCPointZero);

	CCSprite* pStar = CCSprite::create("opening_star@2x.png");
	pStar->setPosition(ccp(size.width / 2, size.height / 2));
	pStar->setScale(0.5f);

	this->addChild(pBg, 0);
	this->addChild(pStar, 1);

	CCScaleTo* scale = CCScaleTo::create(0.5, 1);
	CCFiniteTimeAction* action = CCSequence::create(scale,
			CCCallFunc::create(this,
					callfunc_selector(TitleLayer::starScaleCallBAack)), NULL);
	pStar->runAction(action);


	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

	CCMenuItemImage* closeBut = CCMenuItemImage::create("CloseNormal.png", "CloseSelected.png", this, menu_selector(TitleLayer::menuCloseCallback));
	closeBut->setPosition(ccp(origin.x + size.width - closeBut->getContentSize().width/2 ,
	                                origin.y + closeBut->getContentSize().height/2));

	CCMenuItemFont* testBut = CCMenuItemFont::create("test", this, menu_selector(TitleLayer::menuTestCallback));
	testBut->setPosition(ccp(origin.x + testBut->getContentSize().width/2, origin.y + size.height - testBut->getContentSize().height/2));


//	ccColor4B color = {100, 100, 100, 255};
//
//	CCLayerColor* layerColor = CCLayerColor::create(color, testBut->getContentSize().width, testBut->getContentSize().height);
//	layerColor->setPosition(ccp(0, size.height+testBut->getContentSize().height));
//	this->addChild(layerColor);

	CCMenu* menu = CCMenu::create(closeBut, testBut, NULL);
	menu->setPosition(CCPointZero);

	this->addChild(menu, 5);

	return true;
}

void TitleLayer::starScaleCallBAack() {
	CCSprite* logo = CCSprite::create("opening_logo@2x.png");
	logo->setPosition(
			ccp(CCDirector::sharedDirector()->getVisibleSize().width / 2,
					CCDirector::sharedDirector()->getVisibleSize().height / 2));
	this->addChild(logo, 2);

	CCFadeIn* fadeIn = CCFadeIn::create(0.5);
	logo->runAction(fadeIn);

	CCSprite* bar = CCSprite::create("opening_bar@2x.png");
	bar->setPosition(
			ccp(CCDirector::sharedDirector()->getVisibleSize().width / 2,
					CCDirector::sharedDirector()->getVisibleSize().height / 5));
	this->addChild(bar, 3);
	bar->runAction((CCFadeIn*) fadeIn->copy()->autorelease());

	CCLabelTTF* label = CCLabelTTF::create("开始游戏", "Arial", 40);
	label->setPosition(ccp(CCDirector::sharedDirector()->getVisibleSize().width / 2,
					CCDirector::sharedDirector()->getVisibleSize().height / 5));
	this->addChild(label, 4, 1);

	CCFadeIn* fadeIn2 = CCFadeIn::create(1);
	CCFiniteTimeAction* labelAction = CCSequence::create(fadeIn2, fadeIn2->reverse(), NULL);
	label->runAction(CCRepeatForever::create((CCActionInterval*) labelAction));
}

void TitleLayer::menuCloseCallback(CCObject* pSender)
{
	CCDirector::sharedDirector()->end();
}

void TitleLayer::menuTestCallback()
{
	CCLog("test button be clicked");
}

void TitleLayer::registerWithTouchDispatcher()
{
//	CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, kCCMenuHandlerPriority);
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority, true);
}


void TitleLayer::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
    CCTouch* touch = (CCTouch*)(* pTouches->begin());
    CCPoint pos = touch->getLocation();

    CCLog("2222222222 touch, x = %f, y = %f", pos.x, pos.y);
}

bool TitleLayer::ccTouchBegan(cocos2d::CCTouch * pTouch, cocos2d::CCEvent *pEvent)
{
	CCPoint pos = pTouch->getLocation();

	CCLog(" touch began, x = %f, y = %f", pos.x, pos.y);

	return true;
}


void TitleLayer::ccTouchMoved(cocos2d::CCTouch * pTouch, cocos2d::CCEvent *pEvent)
{
	CCPoint pos = pTouch->getLocation();

	CCLog(" touch moved, x = %f, y = %f", pos.x, pos.y);
}

void TitleLayer::ccTouchEnded(cocos2d::CCTouch * pTouch, cocos2d::CCEvent *pEvent)
{
	CCPoint pos = pTouch->getLocation();

	CCLog(" touch ended, x = %f, y = %f", pos.x, pos.y);

	CCDirector::sharedDirector()->replaceScene(GameScene::create());
}



